<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no,email=no" />
		<title>退款/退货</title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/style.css" rel="stylesheet" />
	</head>
	<style>
		/*#returnQuantity{
			display: none;
		}*/
		.order-info .buy-goods{
			margin: 0;
		}
		#noWorderId,#refundAmount,#refundBalance{
			color:red;
			line-height: 34px;
			display: inline-block
		}
	</style>
	<body>
		<header class="mui-bar mui-bar-nav">
				<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
				<h1 class="mui-title">退款/退货</h1>
		</header>
		
		<!--<div id="scrollDiv" class="scroll-div">-->
				<div id="pullrefresh" class="mui-content transparent" >
					<div id="orderDetail" class="order-info mui-card"></div>
					<div id="ordermsg"  style="margin: 10px 0;">

							<div class="mui-card">
								<div class="mui-input-group">
									<div class="mui-input-row">
										<label>订单编号:</label>
										<p id="noWorderId">0</p>
									</div>
									<div class="mui-input-row">
										<label>可退金豆:</label>
										<p id="refundAmount">0</p>
									</div>
									<div class="mui-input-row">
										<label>可退余额:</label>
										<p id="refundBalance">0</p>
									</div>
								</div>
								
							</div>
							<div class="mui-card" style="margin: 10px 15px;">
								<form class="mui-input-group" id="selectRefundType" data-type="1">
									<div class="mui-input-row mui-checkbox mui-left">
										<label>仅退款</label>
										<input id="refundType_01" value="1" type="checkbox" checked>
									</div>
									<div class="mui-input-row mui-checkbox mui-left">
										<label>退货退款</label>
										<input id="refundType_02" value="2"  type="checkbox" >
									</div>
								</form>
							</div>
							<div class="mui-card" style="margin: 10px 15px;">
								<form class="mui-input-group">
									
									<div class="mui-input-row">
									    <label>联系人:</label>
									    <input id="contactPerson" type="text" placeholder="请填写真实联系姓名" style="font-size:14px ;" data-notempty="请填写联系人">
									</div>
									<div class="mui-input-row">
									    <label>手机号码:</label>
									    <input id="iphone" type="text" placeholder="请填写正确的手机号码" style="font-size:14px ;" data-notempty="请填写手机号码" data-error="请填写正确的手机号码">
									</div>
								
								</form>
							</div>
							<div class="mui-input-row" style="margin: 10px 15px;">
								<textarea id="reason" rows="5" placeholder="退款理由" data-notempty="请填写退货原因" style="font-size:14px ;"></textarea>
							</div>
							
					</div>
					<div  class="action-bar mui-flex comment-btn" style="margin: 10px 15px;">
			            <button class="mui-btn mui-btn-block mui-btn-negative" id="refund-submit" >提交</button>
			        </div>
				</div>
			<!--</div>-->
		<script src="js/mui.min.js"></script>
		<script src="js/jquery-1.8.0.min.js"></script>
		<script src="js/app.js"></script>
		<script>
			mui.init({
				swipeBack: true,
				beforeback:function(){
					mui.fire(plus.webview.getWebviewById('order-list.html'),'updateData');
				},
			});
			mui.plusReady(function(){
				var w=plus.nativeUI.showWaiting('',{padlock:true});
				var 	orderId=plus.webview.currentWebview().orderId;
				var 	itemid=plus.webview.currentWebview().itemid;
				var orderStatus=plus.webview.currentWebview().orderStatus;
				
				if(orderStatus == 2){
					_itemid=-1;
				}else{
					_itemid=itemid
				}
//				console.log(orderStatus)
				mui.ajax(URL+'api/MemberOrder/GetOrderDetail/'+orderId,{
					dataType:'json',
					type:'get',
					timeout:10000,
					success:function(dataDetail){
						console.log(dataDetail)
						if(dataDetail.Success=='true'){
							var html='';
				            	for(var j=0;j<dataDetail.OrderItem.length;j++){
					                html+='<div class="item">'+
					                    '<div class="buy-goods">'+
					                        '<a class="goods-url" data-id="'+dataDetail.OrderItem[j].ProductId+'"><img src="'+dataDetail.OrderItem[j].ProductImage+'"/></a>'+
					                        '<p><em>¥ '+dataDetail.OrderItem[j].Price+'</em><span><a class="goods-url" data-id="'+dataDetail.OrderItem[j].ProductId+'">'+dataDetail.OrderItem[j].ProductName+'</a></span></p>'+
					                        '<h5><span>x '+dataDetail.OrderItem[j].Count+'</span>';
					                        	if(dataDetail.OrderItem[j].size!='')
													html += '尺寸：'+dataDetail.OrderItem[j].size+'&nbsp;&nbsp;';
												if(dataDetail.OrderItem[j].color!='')
													html += '颜色：'+dataDetail.OrderItem[j].color+'&nbsp;&nbsp;';
												if(dataDetail.OrderItem[j].version!='')
													html += '规格：'+dataDetail.OrderItem[j].version;
					                            html+='</h5>'+
					                    '</div>'+
					                '</div>';
				               	}
					          
				            document.getElementById('orderDetail').innerHTML=html;
				       			
	            				//获取
							console.log(URL+'Api/MemberOrder/GetRefundApplySelect?id='+orderId+'&itemid=-1')
							mui.ajax(URL+'Api/MemberOrder/GetRefundApplySelect?id='+orderId+'&itemid=-1',{
									dataType:'json',
									type:'get',
									timeout:10000,
									success:function(data){
										console.log(JSON.stringify(data))
																	
										if(data.Success == "true"){
											document.getElementById("noWorderId").innerHTML = orderId;
											document.getElementById("refundAmount").innerHTML = data.IntegralDiscount+"个";
											document.getElementById("refundBalance").innerHTML = (data.MaxRefundAmount-data.IntegralDiscount).toFixed(2)+"元";
											w.close();
											document.getElementById('pullrefresh').className='mui-content';
											//选择
											var refundTyp1=$("#refundType_01");
											var refundTyp2=$("#refundType_02");
											
											refundTyp1.on("change",tabRefundTyp)
											refundTyp2.on("change",tabRefundTyp)
											function tabRefundTyp(){
												var $red=null,
												bol1=$(this).attr("id") == "refundType_01";
												if(bol1){
													$red=refundTyp2
												}else{
													$red=refundTyp1
												}
												if($(this).is(':checked')){
													$red.prop("checked",false);
													$("#selectRefundType").attr({"data-type":$(this).val()})
													
												}else{
													$red.prop("checked",true);
												}
											}
											
											$("#refund-submit").on("tap",function(){
												var notempty= /^\S+$/;

												//联系人
												if(!notempty.test($("#contactPerson").val())){
													plus.nativeUI.alert($("#contactPerson").attr("data-notempty"))
													return
												}
												
												//手机
												if(!notempty.test($("#iphone").val())){
													plus.nativeUI.alert($("#iphone").attr("data-notempty"))
													return
												}
												if(!(/^0?(13|15|18|14|17)[0-9]{9}$/.test($("#iphone").val()))){
													plus.nativeUI.alert($("#iphone").attr("data-error"))
													return
												}
												
												
												//退款理由

												if(!notempty.test($("#reason").val())){
													plus.nativeUI.alert($("#reason").attr("data-notempty"))
													return
												}
												

												var submintRefundApply={
													OrderId: orderId,
					                                  OrderItemId: itemid,
					                                  RefundType: $("#selectRefundType").attr("data-type"),
					                                  ReturnQuantity: data.MaxRGDNumber,
					                                  Amount: data.MaxRefundAmount,
					                                  Reason: $("#reason").val(),
					                                  ContactPerson: $("#contactPerson").val(),
					                                  ContactCellPhone: $("#iphone").val(),
					                                  RefundPayType: 3
												}
												var submintRefundApplyStr=JSON.stringify(submintRefundApply)
//												console.log(URL+'Api/MemberOrder/GetRefundApply?infoJSON='+submintRefundApplyStr)
												//请求提交
												mui.ajax(URL+'Api/MemberOrder/GetRefundApply?infoJSON='+submintRefundApplyStr,{
													dataType:'json',
													type:'get',
													timeout:10000,
													success:function(submintdata){
//														console.log(JSON.stringify(submintdata))
														if(submintdata.Success){
															plus.nativeUI.alert("申请成功",function(){
																mui.back();
															})
															
														}else{
															plus.nativeUI.alert(submintdata.Message)
														}
																													
													},
													error:function(xhr,type,errorThrown){
//															console.log(JSON.stringify(xhr))
															plus.nativeUI.toast("申请失败，请检查网络")
														}
												})

											})

										}else{
											plus.nativeUI.alert(data.Message,function(){
																mui.back();
															});
											
										}
										
									},
									error:function(xhr,type,errorThrown){
//										console.log(JSON.stringify(xhr))
										plus.nativeUI.toast("网络请求失败,请检查网络")
									}
								});		            
						}
					},
					error:function(xhr,type,errorThrown){
						w.close();
						plus.nativeUI.toast("网络请求失败")
					}
				});

			})
		</script>
	</body>
</html>
